package net.kuratkoo.jidelnicekvut;

import android.content.Context;
import android.telephony.TelephonyManager;
import java.io.IOException;
import org.w3c.dom.Document;

public class Food {
    private static String TAG = "JidelnicekVUT|Food";
    
    public Integer canteenId;
    public String food;
    public Integer price1;
    public Integer price2;
    public Integer price3;
    public String type;
    public String contents;
    public Boolean isKeyword;
    public Float votes;
    private Context context;
    public Integer voteCount;

    public Food(Context context) {
        this.context = context;
        this.price1 = 0;
        this.price2 = 0;
        this.price3 = 0;
        this.votes = Float.parseFloat("0");
        this.voteCount = 0;
        this.contents = "";
        this.isKeyword = false;
    }

    public Boolean vote(Integer vote) throws IOException, Exception {
       
        MyHttpClient mhc = new MyHttpClient(Constants.VOTE_VOTE_URL_API);
        mhc.setPostData("food", this.food);
        mhc.setPostData("canteenId", this.canteenId.toString());
        mhc.setPostData("vote", vote.toString());
        mhc.setPostData("imei", ((TelephonyManager) this.context.getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId());
        mhc.execute();
        Document doc = XML.stringToXml(mhc.getResponse());
        this.votes = Float.parseFloat(XML.getValue(doc.getDocumentElement(), "score"));

        if (XML.getValue(doc.getDocumentElement(), "update").equals("1")) {
            return true;
        } else {
            this.voteCount++;
            return false;
        }
    }
}
